package com.iyohei.action;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.iyohei.entry.Blog;
import com.iyohei.entry.Comment;
import com.iyohei.service.BlogService;
import com.iyohei.service.CommentService;




/**
 * 博客相关action
 * @author Administrator
 *
 */
@RestController
public class BlogIndex {
	
	@Autowired
	private BlogService blogService;
	
	@Autowired
	private CommentService commentService;
	
	/**
	 * 获取首页博客列表
	 */
	@RequestMapping(path="/blog/getById",method= {RequestMethod.GET})
	public List<Blog> getBlog() {
		System.out.println("action进入了");
		List<Blog> blog = blogService.getBlog();
	    return blog;
	}
	
	/**
	 * 根据博客id获取 博客+评论
	 * @param id
	 * @return
	 */
	@RequestMapping(path="/blog/getBlogById/{id}",method= {RequestMethod.GET})
	public Map<String,Object>  getBlogById(@PathVariable("id") long id) {
		Map<String,Object> map = new HashMap();
		Blog blog = blogService.getById(id);
		List<Comment> comList = commentService.getComList(id);
		map.put("blog", blog);map.put("comment", comList);
	    return map;
	}
	
	/**
	 * 根据博客id，插入匿名评论
	 * @param req
	 * @param res
	 * @return
	 */
	@RequestMapping(path="/blog/addCommentNote",method= {RequestMethod.POST})
	public Map<String,Object> addCommentNote(HttpServletRequest req,HttpServletResponse res){
		Map<String,Object> map = new HashMap();
		String id = req.getParameter("id");String commentNote = req.getParameter("comment");
		Comment com = new Comment();
		com.setBlogId(Integer.valueOf(id));com.setCommentName("匿名用户");com.setCommentNote(commentNote);com.setCommentTime(new Date());
		com.setCommentAdmire(0);com.setCommentNumber(0);
		int commentId = commentService.addComment(com);
		if (commentId==1) {
			map.put("commen", com);
		}
		return map;
	}
	
}
